<template>
  <div>
    <div>
      <el-button type="success" size='small' @click="dialogVisible()" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
      <el-button type="primary" size='small' @click="globalDialogVisible()" class="setting">{{$t('全局配置')}}</el-button>
      <span class="tip">{{$t('开启VPN之前需要开启自身防护IPSecVPN开关!')}}</span>
    </div>
    <el-table size='mini' ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55">
      </el-table-column>
      <el-table-column :label="$t('隧道名称')" align='center' min-width="100">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.TunnelName" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column :label="$t('本地IP')" align='center' min-width="150">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.LocalIp" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column :label="$t('远端IP')" align='center' min-width="150">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.RemoteIp" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column :label="$t('本地子网/子网掩码')" min-width="150" align='center'>
        <template slot-scope="scope">
          <tooltip :html="LocalIpv4NetStrFormatter(scope.row,';<br/>')" :content="LocalIpv4NetStrFormatter(scope.row,',')" class="wid190" :refName="scope.row.TunnelName"></tooltip>
        </template>
      </el-table-column>
      <el-table-column :label="$t('远端子网/子网掩码')" min-width="150" align='center'>
        <template slot-scope="scope">
          <tooltip :html="RemoteIpv4NetFormatter(scope.row,';<br/>')" :content="RemoteIpv4NetFormatter(scope.row,',')" class="wid190" :refName="scope.row.TunnelName"></tooltip>
        </template>
      </el-table-column>
      <el-table-column :label="$t('预共享秘钥')" align='center' min-width="150">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.PreSharedKey" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column :label="$t('隧道状态')" align='center' min-width="100">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.TunnelStatus" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="sys" fixed="right" :label="$t('操作')" align='center' width="250">
        <template slot-scope="scope">
          <el-switch :width="lang === 'zh'?56:70" style="margin-right:7px;" class="sysSwitch switch" v-model="scope.row.TunnelOpenOrClose" active-color="#009966" inactive-color="#CCCCCC" active-value="open" inactive-value="close" :active-text="$t('开启')" :inactive-text="$t('关闭')" @change='changeStatus(scope.row, scope.row.TunnelOpenOrClose)'></el-switch>
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row)" :class="'edit' +(scope.$index+1)">
            {{$t('编辑')}}
          </el-button>
          <el-button type="primary" plain size="mini" @click="checkDialog(scope.row)" :class="'finds' +(scope.$index+1)">
            {{$t('查看')}}
          </el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-size="size" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>

    <ipsecvpnDialog v-if="ipsecvpnDialogVisible" ref="refIpsecvpnDialogVisible"></ipsecvpnDialog>
    <ipsecvpncheck v-if="checkDialogVisible" ref="refCheckDialogVisible"></ipsecvpncheck>
    <globalDialog v-if="globalDialogFlag" ref="refGlobalDialogVisible"></globalDialog>
  </div>
</template>
<script>
import { getArray, switchStatus, del_info } from '@/api/network/ipsecvpn'
import ipsecvpnDialog from './components/ipsecvpn'
import Ipsecvpncheck from './components/ipsecvpnCheck'
import globalDialog from './components/globalDialog'
export default {
  name: 'Tunnel',
  components: {
    ipsecvpnDialog,
    Ipsecvpncheck,
    globalDialog
  },
  data () {
    return {
      ipsecvpnDialogVisible: false,
      checkDialogVisible: false,
      globalDialogFlag: false,
      TunnelOpenOrClose: true,
      tableData: [],
      tableDataAll: [],
      lang: this.$i18n.locale,
      name: '',
      form: {
        name: 'test',
        category_info: [
          {
            'action': 'drop',
            'name': 'TCP FLOOD',
            'protype': 'tcp',
            'sid': 0,
            'status': 'on',
            'threshold': 2000,
            'type': 'detect',
            'unit': 'pps',
            'window': 200
          }
        ]
      },
      total: null,
      page: 1,
      size: 20
    }
  },
  created () {
    this.listData()
  },
  methods: {
    handleSizeChange (val) {
      this.size = val
      this.pageListData()
    },
    handleCurrentChange (val) {
      this.page = val
      this.pageListData()
    },
    pageListData () {
      let newPage = Math.ceil(this.total / this.size)
      if (newPage < this.page) {
        this.page = newPage
      }
      if (this.page === 0) {
        this.tableData = this.tableDataAll
      } else {
        this.tableData = this.tableDataAll.slice((this.page - 1) * this.size, this.page * this.size)
      }
    },
    // 多选按钮
    handleSelectionChange (data) {
      var name = ''
      for (var item of data) {
        if (name === '') {
          name = item.TunnelName
        } else {
          name = name + '@#@' + item.TunnelName
        }
      }
      this.name = name
    },
    // 列表
    listData () {
      getArray().then(res => {
        if (res.status === 0) {
          this.tableData = res.info
          this.tableDataAll = res.info
          this.total = this.tableDataAll.length
          this.pageListData()
        }
      })
    },
    // 添加按钮表单
    dialogVisible (data) {
      this.ipsecvpnDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refIpsecvpnDialogVisible.init(data)
      })
    },
    // 查看按钮表单
    checkDialog (data) {
      this.checkDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refCheckDialogVisible.init(data)
      })
    },
    globalDialogVisible () {
      this.globalDialogFlag = true
      this.$nextTick(() => {
        this.$refs.refGlobalDialogVisible.init()
      })
    },
    deleteData () {
      if (this.name) {
        this.$confirm(this.$t('您是否要删除所有选中的数据?'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          del_info({ name: this.name }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.name = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    changeStatus (row, callback) {
      switchStatus({ id: row.Index, state: callback === 'open' ? 'enable' : 'disable' }).then(res => {
        if (res.status === 0) {
          this.$message({
            message: this.$t(res.msg),
            type: 'success'
          })
        }
      }).catch(e => {
        row.TunnelOpenOrClose = row.TunnelOpenOrClose === 'open' ? 'close' : 'open'
      })
    },
    LocalIpv4NetStrFormatter (row, str) {
      let LocalIpNet = ''
      if (row.LocalIpNet) {
        row.LocalIpNet.forEach((item, index) => {
          if (index === 0 && str === ',') {
            LocalIpNet += item.IpNet + '/' + item.Netmask
          } else if (str === ',') {
            LocalIpNet += str + item.IpNet + '/' + item.Netmask
          } else {
            LocalIpNet += item.IpNet + '/' + item.Netmask + str
          }
        })
      }
      // console.log(LocalIpNet)
      return LocalIpNet
    },
    RemoteIpv4NetFormatter (row, str) {
      let RemoteIpNet = ''
      if (row.RemoteIpNet) {
        row.RemoteIpNet.forEach((item, index) => {
          if (index === 0 && str === ',') {
            RemoteIpNet += item.IpNet + '/' + item.Netmask
          } else if (str === ',') {
            RemoteIpNet += str + item.IpNet + '/' + item.Netmask
          } else {
            RemoteIpNet += item.IpNet + '/' + item.Netmask + str
          }
        })
      }
      return RemoteIpNet
    }
  }
}
</script>

<style lang="less" scoped>
.tip {
  color: red;
  position: fixed;
  right: 0;
  padding-right: 3%;
}
</style>
